/**
 * Created by meiguizhinian on 2018/6/25
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//cocos2dx html版本不支持二进制fnt加载
if (!cc.sys.isNative) {
	_fntLoader.parseBinaryFnt = function (fntData, url) {
		//common
		let fnt = {
			commonHeight : 0,
			fontDefDictionary : {},
			kerningDict : {},
		};

		let pData = new ivy.InputStream(fntData);
		pData.move(3);
		cc.assert(pData.readInt8()===3,"Only version 3 is supported");
		while (!pData.finish()) {
			let blockId = pData.readUInt8();
			let blockSize = pData.readUInt32();

			if (blockId===1) {
				fnt.fontSize = pData.readInt16();

				pData.move(5);
				let padding = {};
				padding.top = pData.readUInt8();
				padding.right = pData.readUInt8();
				padding.bottom = pData.readUInt8();
				padding.left = pData.readUInt8();
				fnt.padding = padding;

				pData.move(blockSize-11);
			} else if (blockId===2) {
				fnt.commonHeight = pData.readUInt16();

				pData.move(2);
				let scaleW = pData.readUInt16();
				let scaleH = pData.readUInt16();
				cc.assert(scaleW<=cc.configuration.getMaxTextureSize() && scaleH<=cc.configuration.getMaxTextureSize(), "CCLabelBMFont: page can't be larger than supported");
				let pages = pData.readUInt16();
				cc.assert(pages === 1, "CCBitfontAtlas: only supports 1 page");

				pData.move(blockSize-10);
			} else if (blockId===3) {
				let filename = pData.readStringUTF8(blockSize-1);
				fnt.atlasName = cc.path.changeBasename(url, filename);

				pData.move(1);
			} else if (blockId===4) {
				let count = blockSize/20;

				for (let i=0; i!==count; ++i) {
					//pData.move(i*20);
					let charId = pData.readUInt32();

					let dict = {};
					dict.rect = {
						x : pData.readUInt16(),
						y : pData.readUInt16(),
						width : pData.readUInt16(),
						height : pData.readUInt16(),
					};
					dict.xOffset = pData.readInt16();
					dict.yOffset = pData.readInt16();
					dict.xAdvance = pData.readInt16();

					fnt.fontDefDictionary[charId.toString()] = dict;

					pData.move(2);
				}
			} else if (blockId===5) {
				let count = blockSize/20;
				for (let i=0; i!==count; ++i) {
					let first = pData.readUInt32();
					let second = pData.readUInt32();
					let amount = pData.readInt16();

					let key = (first<<32) | (second&0xffffffff);
					fnt.kerningDict[key] = amount;
				}
			}
		}
		return fnt;
	};
	//由于fnt有二进制格式的文件，所以使用二进制加载文件
	_fntLoader.load = function(realUrl, url, res, cb) {
		cc.loader.loadBinary(realUrl, function (err, data) {
			if (err) return cb(err);
			if (data[0]===66 && data[1]===77 && data[2]===70) {//BMF 的二进制编码
				cb(null, this.parseBinaryFnt(data, url));
			}  else {
				cb(null, this.parseFnt(String.fromCharCode.apply(String, data), url));
			}
		}.bind(this));
	};
} else {
	cc.loader.loadTxt = function(url, cb){
		return cb(null, jsb.fileUtils.getStringFromFile(url));
	};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
cc._csvLoader = {
	load: function (realUrl, url, res, cb) {
		return cc.loader.loadTxt(realUrl, function (err, txt) {
			if (err) {
				cb && cb(err);
			} else {
				try {
					let array = txt.split(/\r\n|\n/);
					let result = [];
					for (let a of array) {
						let aa = a.split(",");
						result.push(aa);
					}
					cb && cb(null, result);
					return result;
				} catch (e) {
					throw new Error("parse csv [" + url + "] failed : " + e);
				}
			}
		});
	}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

cc.loader.register(["actor", "bin", "config", "sp"], cc._binaryLoader);
cc.loader.register(["csv"], cc._csvLoader);
